<?php
/**
 * 以物换物
 */
namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\EditorModel;
use Common\Model\ExchangeModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Think\Page;

class ExchangeController extends BaseController{

	/**
	 * 物品列表页
	 * Author: shijy
	 */
	public function itemList(){
		$type		=I('post.itemType',0,'int');
		$status		=I('post.itemStatus',0,'int');
		$pageSize	=I('post.limit',10,'int');
		$page		=I('post.p',1,'int');


		$itemStatus = ExchangeModel::getItemStatus();
		$this->smarty->assign('itemStatus',$itemStatus);

		$itemType = ExchangeModel::getItemType();
		$this->smarty->assign('itemType',$itemType);

		$list=ExchangeModel::getItemList($status,$type,$page,$pageSize,0);

		fetchAjaxPager($this->smarty,$list,$pageSize);

		$this->smarty->assign('ITEM_STATUS_ONLINE',ExchangeModel::ITEM_STATUS_ONLINE);
		$this->smarty->assign('ITEM_STATUS_IN_TRADING',ExchangeModel::ITEM_STATUS_IN_TRADING);
		$this->smarty->assign('ITEM_STATUS_OFFLINE',ExchangeModel::ITEM_STATUS_OFFLINE);
		$this->smarty->assign('ITEM_STATUS_SUCCESS',ExchangeModel::ITEM_STATUS_SUCCESS);
		$this->smarty->assign('ITEM_STATUS_AUDIT',ExchangeModel::ITEM_STATUS_AUDIT);
		$this->smarty->assign('ITEM_STATUS_AUDIT_REFUSE',ExchangeModel::ITEM_STATUS_AUDIT_REFUSE);

		if(IS_AJAX){


			$this->smarty->assign('list',$list['data']);
			$listHtml=$this->smarty->fetch('Exchange/item_list.html');


			$pagerHtml=$this->smarty->fetch('Public/page_ajax.html');
			$this->ajaxReturn(array(
				'status'=>0,
				'listHTML'=>$listHtml,
				'pagerHTML'=>$pagerHtml
			));

		}else{
			$this->smarty->assign('list',$list['data']);
			$this->smarty->display('Exchange/item.html');
		}

	}

	/**
	 * 交换记录
	 */
	public function tradeList(){
		$status		=I('post.recordStatus',0,'int');
		$pageSize	=I('post.limit',10,'int');
		$page		=I('post.p',1,'int');

		$nickname 	=I('post.nickname','');
		$beginTime	=I('post.beginTime','');
		$endTime	=I('post.endTime','');




		$recordStatus = ExchangeModel::getRecordStatus();
		$this->smarty->assign('recordStatus',$recordStatus);

		$itemType = ExchangeModel::getItemType();
		$this->smarty->assign('itemType',$itemType);

		$list=ExchangeModel::getTradeList($status,$page,$pageSize,0,$nickname,$beginTime,$endTime);
		fetchAjaxPager($this->smarty,$list,$pageSize);

		$this->smarty->assign('list',$list['data']);
		

		if(IS_AJAX){
			$listHtml=$this->smarty->fetch('Exchange/trade_list.html');

			$pagerHtml=$this->smarty->fetch('Public/page_ajax.html');
			$this->ajaxReturn(array(
				'status'=>0,
				'listHTML'=>$listHtml,
				'pagerHTML'=>$pagerHtml
			));

		}else{
			$this->smarty->display('Exchange/trade.html');
		}

	}


	/**
	 * 审核拒绝
	 */
	public function adminRefuse(){
		$id=I('post.id',0,'intval');
		$reason=I('post.reason');

		$ret=ExchangeModel::getItemDetail($id);
		if($ret['status']!=0){
			$this->ajaxReturn($ret);
		}
		$item=$ret['data'];
		switch ($item['status']){
			case ExchangeModel::ITEM_STATUS_AUDIT:
			case ExchangeModel::ITEM_STATUS_ONLINE:
				break;
			default:
				$ret['msg']='当前状态下不可改为审核不通过';
				$this->ajaxReturn($ret);
				break;
		}
		ExchangeModel::getItemInstance()->where('id='.$id)->save(
			array(
				'status'=>ExchangeModel::ITEM_STATUS_AUDIT_REFUSE,
				'auditReason'=>$reason
			)
		);
		$this->ajaxReturn(array('status'=>0,'msg'=>'操作成功'));

	}

	/**
	 * 审核通过
	 */
	public function adminPass(){
		$id=I('post.id',0,'intval');

		$ret=ExchangeModel::getItemDetail($id);
		if($ret['status']!=0){
			$this->ajaxReturn($ret);
		}
		$item=$ret['data'];
		switch ($item['status']){
			case ExchangeModel::ITEM_STATUS_AUDIT:
				break;
			default:
				$ret['msg']='当前状态下不可改为审核不通过';
				$this->ajaxReturn($ret);
				break;
		}
		ExchangeModel::getItemInstance()->where('id='.$id)->save(
			array(
				'status'=>ExchangeModel::ITEM_STATUS_ONLINE,
				'auditTime'=>time(),
				'album'=>implode(',',I('post.album')),
				'desc'=>I('post.desc'),
				'intention'=>I('post.intention'),
				'itemType'=>I('post.itemType',0,'intval'),
			)
		);
		//后台审核通过不加积分
		///UserModel::addUserValByAction($item['ownerId'],OPT_TOY_PUBLISH);
		$this->ajaxReturn(array('status'=>0,'msg'=>'操作成功'));

	}
}
